package org.example.blog.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.blog.constants.Constants;
import org.example.blog.model.BlogInfo;
import org.example.blog.service.BlogService;
import org.example.blog.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        blogService.getList();
        return   blogService.getList();

    }
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request){
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        log.info("getBlogDetail,blogId:{}",blogId);
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        log.info("获取token:{}",token);
        //从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        log.info("获取用户ID:{}",userId);
        if(userId != null && userId == blogDetail.getUserId()){
            blogDetail.setLoginUser(true);
        }else{
            blogDetail.setLoginUser(false);
        }
       return  blogDetail;
    }

    
    //写博客
    @RequestMapping("/addBlog")
    public Boolean addBolg(@RequestBody BlogInfo blogInfo,HttpServletRequest request){
        log.info("添加博客:{}",blogInfo);
        //参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        //获取写博客的用户信息 即此时登录的用户信息
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId <= 0){
            return false;
        }

        blogInfo.setUserId(userId);
        return blogService.insertBlog(blogInfo);

    }

    @RequestMapping("/updateBlog")
    public Boolean updateBlog(BlogInfo blogInfo){
        log.info("更新博客:{}",blogInfo);
        //参数校验
        if(blogInfo.getId() == null || !StringUtils.hasLength(blogInfo.getTitle())
        ||!StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        blogService.update(blogInfo);
        return true;
        
    }
    @RequestMapping("/deleteBlog")
    public Boolean deleteBlog(Integer blogId) {
        log.info("删除博客:}",blogId);
        if(blogId <= 0 ){
            return  false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        blogService.update(blogInfo);
        return true;

    }
}
